﻿@model Model.ModelConfig
@{
    ViewBag.Title = "编辑接口配置";
    Layout = "~/Views/Shared/_Main.cshtml";
}
<style>
    select
    {
        width: 150px;
    }
</style>
<script type="text/javascript">
    var modelID = "";
    function Save() {
        if (modelID == "") {
            alert("请选择模型！");
            return false;
        }
        var postArrData = new Array();
        var postExtData = new Array();
        $(".columnSync tr").each(function (i, d) {
            var trArr = jv.GetDivArray($(this), 3);
            if (trArr.UserColumnName)
                postArrData.push(trArr);
        });
        $(".columnExt tr").each(function (i, d) {
            var trArr = jv.GetDivArray($(this), 3);
            if (trArr.UserColumnName) {
                postExtData.push(trArr);
            }
        });
        $.post("/SysUser/SaveUserIF", { query: $.toJSON(postArrData), queryExt: $.toJSON(postExtData), modelID: modelID }, function (res) {
            if (res == 1) {
                alert("操作成功！");
            } else
                alert(res);
        });
    }
    function ModelTableInfo(v, t) {
        showModalMany('Info', '/ODSConfig/TableInfo?modelID=' + v + '&tbName=' + t, '900', '480');
    }
    function returnModel(v) {
        modelID = v[0].GUID;
        $("#ModelName").html(v[0].TableName);
        initPage(v[0].GUID, 'reload');
    }
    function ChooseModel() {
        showModalMany('Contact', '/ModelConfig/ModelList?reference=1', '900', '480', returnModel);
    }
    var coli = 1;
    var modelColumn = new Array();
    function AddColumn() {
        $(".columnExt").append("<tr class='tr" + coli + "'><td width='260'> 字段定义：<input name='txtUserColumnName' value='COL" + coli +
        "' type='hidden' /><input name='txtColumnDefine' maxlength='50' value='' type='text' /></td><td> 模型字段：<select class='modelColumn' name='selModelColumnName'><option value=''>-请选择-</option>" +
        modelColumn.join("")
        + "</select> <input type='button' class='button' value='删除' onclick='DelColumn(" + coli + ")' /></td></tr>");
        coli++;
    }
    function DelColumn(v) {
        $(".tr" + v).remove();
    }
    function initPage(id, loadType) {
        $(".modelColumn").empty();
        modelColumn = [];
        $(".modelColumn").append("<option value=''>-请选择-</option>");
        $.post('/ODSConfig/GetTableInfo', { modelID: id }, function (result) {
            for (var i = 0; i < result.rows.length; i++) {
                var v = "<option value='" + result.rows[i].ColumnName + "'>" + result.rows[i].ColumnName + "</option>";
                $(".modelColumn").append(v);
                modelColumn.push(v);
            }
            if (loadType == "load") {
                $.post("/PEPara/GetUserIFConfig", function (res) {
                    if (res.length > 0) {
                        for (var i = 0; i < res.length; i++) {
                            var setValue = { UserColumnName: res[i].UserColumnName, ColumnDefine: res[i].ColumnDefine, ModelColumnName: res[i].ModelColumnName };
                            if ($("." + res[i].UserColumnName).length > 0) {
                                $("." + res[i].UserColumnName).find("input,select").each(function (j, d) {
                                    var nm = d.name;
                                    nm = nm.slice(3);
                                    $(d).val(setValue[nm]);
                                });
                            } else {
                                AddColumn();
                                var setColi = coli - 1;
                                $(".tr" + setColi).find("input,select").each(function (j, d) {
                                    var nm = d.name;
                                    if (jv.IsNull(nm)) return;
                                    nm = nm.slice(3);
                                    $(d).val(setValue[nm]);
                                });
                            }
                        }
                    }
                });
            }
            else if (loadType == "reload") {
                $(".columnExt").empty();
            }
        });
    }
    $(function () {
        $.post("/PEPara/GetSysConfig", { type: 2 }, function (res) {
            if (res.length > 0) {
                modelID = res[0].KeyValue;
                initPage(res[0].KeyValue, 'load');
                $("#ModelName").html("<a onclick='ModelTableInfo(\"" + modelID + "\",\"" + '@Model.TableName' + "\");' href=\"#\">" + '@Model.TableName' + "</a>");
            }
        });

    })
</script>
<div style="margin: 5px;">
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                编辑接口配置
            </td>
            <td align="right">
                <input type="button" class="button" value=" 选择模型 " onclick="ChooseModel()" />
                <input type="button" class="button" value=" 保存 " onclick="Save()" />
            </td>
        </tr>
    </table>
    <table class="searchArea" width="100%" height="50">
        <tr>
            <td>
                模型名称：<span id="ModelName"></span>
            </td>
        </tr>
    </table>
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                系统字段
            </td>
            <td align="right">
            </td>
        </tr>
    </table>
    <table class="searchArea columnSync" width="100%">
        <tr class="Username">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Username" type="hidden" /><input disabled
                    name="txtColumnDefine" value="用户名" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="Pwd">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Pwd" type="hidden" /><input disabled
                    name="txtColumnDefine" value="密码" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="Mobile">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Mobile" type="hidden" /><input disabled
                    name="txtColumnDefine" value="手机" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="OfficeEmail">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="OfficeEmail" type="hidden" /><input disabled
                    name="txtColumnDefine" value="邮箱" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="RTXID">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="RTXID" type="hidden" /><input disabled
                    name="txtColumnDefine" value="RTX账号" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="EmployeeName">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="EmployeeName" type="hidden" /><input
                    disabled='disabled' name="txtColumnDefine" value="姓名" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="Gender">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Gender" type="hidden" /><input disabled
                    name="txtColumnDefine" value="性别" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="EmployeeCode">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="EmployeeCode" type="hidden" /><input
                    disabled='disabled' name="txtColumnDefine" value="员工编码" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="OfficeTel">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="OfficeTel" type="hidden" /><input disabled
                    name="txtColumnDefine" value="办工电话" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="HomeTel">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="HomeTel" type="hidden" /><input disabled
                    name="txtColumnDefine" value="家庭电话" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="CompanyName">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="CompanyName" type="hidden" /><input disabled
                    name="txtColumnDefine" value="公司" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="Department">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Department" type="hidden" /><input disabled
                    name="txtColumnDefine" value="部门" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
        <tr class="Post">
            <td width="260">
                字段定义：<input name="txtUserColumnName" value="Post" type="hidden" /><input disabled
                    name="txtColumnDefine" value="岗位" type="text" />
            </td>
            <td>
                模型字段：<select class="modelColumn" name="selModelColumnName"><option value=''>-请选择-</option>
                </select>
            </td>
        </tr>
    </table>
    <table class="listTitle">
        <tr>
            <td width="15">
                <img src="/images/edit.png" />
            </td>
            <td width="300">
                扩展字段
            </td>
            <td align="right">
                <input type="button" class="button" value="添加" onclick="AddColumn()" />
            </td>
        </tr>
    </table>
    <table class="searchArea columnExt" width="100%">
    </table>
</div>
